package com.tanhua.api.mongo;

import com.tanhua.mongo.Comment;
import com.tanhua.mongo.Publish;
import com.tanhua.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service
public class CommentApiImpl implements CommentApi {

     @Autowired
     private MongoTemplate mongoTemplate;

    public Integer save(Comment comment) {
        //保存数据到comment表形成一条记录
        comment.setCreated(System.currentTimeMillis());
        //根据动态id查询动态发布人的id
        Publish publish = mongoTemplate.findById(comment.getPublishId(), Publish.class);
        comment.setPublishUserId(publish.getUserId()); //发布动态人的id
        mongoTemplate.insert(comment);  //保存

        // 修改publish动态表（累加点赞评论或喜欢行为）
        Query query = new Query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        String type = comment.getCol();
        update.inc(type,1); //自增
        mongoTemplate.upsert(query,update,Publish.class);

        // 查询点赞、喜欢、评论数
        Query countquery = new Query(Criteria.where("pubType").is(comment.getPubType())
                .and("commentType").is(comment.getCommentType()).and("publishId").is(comment.getPublishId()));
        Long count = mongoTemplate.count(countquery, Comment.class);
        return count.intValue();
    }

    @Override
    public Integer delete(Comment comment) {

        Query qr = new Query(
                Criteria.where("publishId").is(comment.getPublishId())
                        .and("commentType").is(comment.getCommentType())
                        .and("userId").is(comment.getUserId())
        );
        mongoTemplate.remove(qr,Comment.class);  //保存

        // 修改publish动态表（累加点赞评论或喜欢行为）
        Query query = new Query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        String type = comment.getCol();
        update.inc(type,-1); //自增
        mongoTemplate.upsert(query,update,Publish.class);

        // 查询点赞、喜欢、评论数
        Query countquery = new Query(Criteria.where("pubType").is(comment.getPubType())
                .and("commentType").is(comment.getCommentType()).and("publishId").is(comment.getPublishId()));
        Long count = mongoTemplate.count(countquery, Comment.class);
        return count.intValue();
    }

    @Override
    public PageBeanVo findInfo(Integer page, Integer pagesize, String publishId) {

        Query query = new Query(Criteria.where("publishId").is(new ObjectId(publishId)).
                and("pubType").is(1).and("commentType").is(2));
        query.with(new Sort(Sort.Direction.DESC,"created"));
        query.skip( (page-1)*pagesize ).limit(pagesize);
        List<Comment> list = mongoTemplate.find(query, Comment.class);
        Long count = mongoTemplate.count(query, Comment.class);
        return new PageBeanVo(page,pagesize,count,list);
    }

    @Override
    public PageBeanVo findPage(Integer page, Integer pagesize, int commentType, Long userId) {

        Query query = new Query(Criteria.where("commentType").is(commentType).and("publishUserId").is(userId));
        query.with(new Sort(Sort.Direction.DESC,"created"));
        query.skip((page-1)*pagesize).limit(pagesize);
        List<Comment> list = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        return new PageBeanVo(page,pagesize,count,list);
    }
}
